/*
 * LoginFrame.java
 *
 * Created on 14. Juni 2008, 18:02
 */

package ads.client;

import javax.swing.JOptionPane;

import ads.exception.PlayingGameException;
import ads.exception.ServerRemoteException;
import ads.util.ClientConfig;

/**
 * 
 * @author xian
 */
public class LoginFrame extends javax.swing.JFrame {
	private ClientImpl client;

	/** Creates new form LoginFrame */
	public LoginFrame() {
		initComponents();
		postComponents();
	}

	private void postComponents() {
		ClientConfig config = ClientConfig.load();
		this.jTextFieldServer.setText(config.toString());
		this.jTextFieldClientPort.setText(config.getClientPort().toString());

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabelUserName = new javax.swing.JLabel();
        jLabelServer = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jTextFieldServer = new javax.swing.JTextField();
        jButtonLogin = new javax.swing.JButton();
        jButtonReLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldClientPort = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabelUserName.setText("Username:");

        jLabelServer.setText("Server:");

        jButtonLogin.setText("Login");
        jButtonLogin.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoginActionPerformed(evt);
            }
        });
        jButtonReLogin.setText("Relogin");
        jButtonReLogin.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonReLoginActionPerformed(evt);
            }
        });
        jLabel1.setText("Client port:*");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addGroup(
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addGroup(
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jLabelUserName).addComponent(
                jLabelServer).addComponent(
                jLabel1)).addPreferredGap(
                javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.TRAILING).addComponent(
                jTextFieldUsername,
                javax.swing.GroupLayout.Alignment.LEADING,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                342,
                Short.MAX_VALUE).addComponent(
                jTextFieldClientPort,
                javax.swing.GroupLayout.Alignment.LEADING,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                342,
                Short.MAX_VALUE).addComponent(
                jTextFieldServer,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                342,
                Short.MAX_VALUE))).addGroup(
                layout.createSequentialGroup().addGap(
                169,
                169,
                169).addComponent(
                jButtonLogin).addComponent(jButtonReLogin))).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addGroup(
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                jLabelUserName).addComponent(
                jTextFieldUsername,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
                javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.TRAILING).addGroup(
                javax.swing.GroupLayout.Alignment.LEADING,
                layout.createSequentialGroup().addGap(
                32,
                32,
                32).addGroup(
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                jLabelServer).addComponent(
                jTextFieldServer,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE))).addGroup(
                javax.swing.GroupLayout.Alignment.LEADING,
                layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                jTextFieldClientPort,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                jLabel1))).addGap(18, 18, 18).addComponent(jButtonLogin).addComponent(jButtonReLogin).addContainerGap(
                javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents
	private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonLoginActionPerformed
		ClientConfig config = ClientConfig.parse(jTextFieldServer.getText(),
				jTextFieldClientPort.getText());
		if (config == null) {
			JOptionPane.showMessageDialog(this, "invalid server.should be:\n"
					+ "rmi://127.0.0.1:1099/serverName.\n"
					+ "clientport should be a integer");
			return;
		}
		String username = jTextFieldUsername.getText();
		if (username == null || username.isEmpty()) {
			JOptionPane.showMessageDialog(this, "username should not be null");
			return;
		}
		try {
			client = new ClientImpl(config);
			client.login(username);
			GamesFrame gamesFrame = new GamesFrame(client);
			this.setVisible(false);
			gamesFrame.setVisible(true);
		} catch (ServerRemoteException e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		} catch (PlayingGameException e) {
			JOptionPane.showMessageDialog(this, e);
		}

	}// GEN-LAST:event_jButtonLoginActionPerformed
	private void jButtonReLoginActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonLoginActionPerformed
		ClientConfig config = ClientConfig.parse(jTextFieldServer.getText(),
				jTextFieldClientPort.getText());
		if (config == null) {
			JOptionPane.showMessageDialog(this, "invalid server.should be:\n"
					+ "rmi://127.0.0.1:1099/serverName.\n"
					+ "clientport should be a integer");
			return;
		}
		String username = jTextFieldUsername.getText();
		if (username == null || username.isEmpty()) {
			JOptionPane.showMessageDialog(this, "username should not be null");
			return;
		}
		try {
			client = new ClientImpl(config);
			client.relogin(username);
			GamesFrame gamesFrame = new GamesFrame(client);
			this.setVisible(false);
			gamesFrame.setVisible(true);
		} catch (ServerRemoteException e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		} catch (PlayingGameException e) {
			JOptionPane.showMessageDialog(this, e);
		}

	}// GEN-LAST:event_jButtonLoginActionPerformed
	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new LoginFrame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButtonLogin;
	private javax.swing.JButton jButtonReLogin;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabelServer;
	private javax.swing.JLabel jLabelUserName;
	private javax.swing.JTextField jTextFieldClientPort;
	private javax.swing.JTextField jTextFieldServer;
	private javax.swing.JTextField jTextFieldUsername;
	// End of variables declaration//GEN-END:variables

}
